﻿L.Control.Basemaps = L.Control.extend({
    _map: null,
    includes: L.Evented ? L.Evented.prototype : L.Mixin.Event,
    options: {
        position: 'bottomright'
    },
    basemap: "map",
    onAdd: function (map) {
        this._map = map;
        var container = L.DomUtil.create('div', 'basemaps leaflet-control closed');
        // disable events
        L.DomEvent.disableClickPropagation(container);
        if (!L.Browser.touch) {
            L.DomEvent.disableScrollPropagation(container);
        }
        this.addMapgroup(container);
        this.addSattelitegroup(container);
        this.addBlueMapgroup(container);
        this.showBaseMap();
        this._container = container;
        return this._container;
    },

    addMapgroup: function (container) {
        //加载地图
        var basemapClass = 'basemap map';
        var basemapNode = L.DomUtil.create('div', basemapClass, container);
        var imgNode = L.DomUtil.create('img', null, basemapNode);
        imgNode.src = "../../../img/basemap/worldmap.jpg";
        imgNode.title = "地图";
        this.mapgroup = new L.layerGroup();



        L.tileLayer(" http://t{s}.tianditu.gov.cn/vec_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=b06b0ce1afd9700fb8f5960311f98ac7", {
            maxZoom: 18,
            minZoom: 0,
            continuousWorld: true,
            opacity: 0.5,
            label: '地图',
            subdomains: ["0", "1", "2", "3", "4", "5", "6", "7"]
        }).addTo(this.mapgroup);

        //公路
        L.tileLayer("http://47.105.99.44/ArcGIS/rest/services/OneMapNanTong/MapServer/tile/{z}/{y}/{x}", {
            maxZoom: 18,
            minZoom: 0,
            continuousWorld: true,
            zoomOffset: 0,
            label: '地图',
            attribution: '江苏公路'
        }).addTo(this.mapgroup); 



        this.mapgroup.addTo(this._map);

        L.DomEvent.on(basemapNode, 'click', function () {
            //if different, remove previous basemap, and add new one
            this.basemap = "map";
            this.showBaseMap();
            L.DomUtil.addClass(document.getElementsByClassName('basemap satellite')[0], 'alt');
            L.DomUtil.removeClass(basemapNode, 'alt');
        }, this);

    },

    addSattelitegroup: function (container) {
        //加载影像按钮
        var basemapClass = 'basemap satellite';
        if (this.basemap = "map")
            basemapClass += ' alt';
        var basemapNode = L.DomUtil.create('div', basemapClass, container);
        var imgNode = L.DomUtil.create('img', null, basemapNode);
        imgNode.src = "../../../img/basemap/worldimage.jpg";
        imgNode.title = "影像";

        this.satellitegroup = L.layerGroup();

        L.tileLayer(" http://t{s}.tianditu.gov.cn/img_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=c&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=b06b0ce1afd9700fb8f5960311f98ac7", {
            maxZoom: 18,
            minZoom: 0,
            continuousWorld: true,
            label: '影像',
            subdomains: ["0", "1", "2", "3", "4", "5", "6", "7"]
        }).addTo(this.satellitegroup);
        //公路
        L.tileLayer("http://47.105.99.44/ArcGIS/rest/services/OneMapNanTong/MapServer/tile/{z}/{y}/{x}", {
            maxZoom: 18,
            minZoom: 0,
            continuousWorld: true,
            zoomOffset: 0,
            label: '地图',
            attribution: '江苏公路'
        }).addTo(this.satellitegroup); 

        this.satellitegroup.addTo(this._map);



        L.DomEvent.on(basemapNode, 'click', function () {
            //if different, remove previous basemap, and add new one
            this.basemap = "satellite";
            this.showBaseMap();

            L.DomUtil.addClass(document.getElementsByClassName('basemap bluemap')[0], 'alt');
            L.DomUtil.removeClass(basemapNode, 'alt');
        }, this);

    },

    addBlueMapgroup: function (container) {
        //加载影像按钮
        var basemapClass = 'basemap bluemap';
        //if (this.basemap = "map")
        //    basemapClass += ' alt';
        var basemapNode = L.DomUtil.create('div', basemapClass, container);
        var imgNode = L.DomUtil.create('img', null, basemapNode);
        imgNode.src = "../../../img/basemap/worldmapblue.jpg";
        imgNode.title = "午夜蓝图";

        this.bluemapgroup = L.layerGroup();

        //山东公路
        L.tileLayer("http://47.105.99.44/ArcGIS/rest/services/OneMapNanTongBlue/MapServer/tile/{z}/{y}/{x}", {
            maxZoom: 18,
            minZoom: 0,
            continuousWorld: true,
            zoomOffset: 0,
            label: '午夜蓝图',
            attribution: '江苏公路'
        }).addTo(this.bluemapgroup); 

        this.bluemapgroup.addTo(this._map);



        L.DomEvent.on(basemapNode, 'click', function () {
            //if different, remove previous basemap, and add new one
            this.basemap = "bluemap";
            this.showBaseMap();

            L.DomUtil.addClass(document.getElementsByClassName('basemap map')[0], 'alt');
            L.DomUtil.removeClass(basemapNode, 'alt');
        }, this);

    },


    showBaseMap: function () {
        if (this.basemap === "map") {
            this._map.removeLayer(this.satellitegroup);
            this._map.removeLayer(this.bluemapgroup);
            this._map.addLayer(this.mapgroup);
            this._map.fire('baselayerchange', this.satellitegroup);
            this._map.fire('baselayerchange', this.bluemapgroup);
        }
        else if (this.basemap === "satellite"){
            this._map.removeLayer(this.mapgroup);
            this._map.removeLayer(this.bluemapgroup);
            this._map.addLayer(this.satellitegroup);
            this._map.fire('baselayerchange', this.mapgroup);
            this._map.fire('baselayerchange', this.bluemapgroup);
        }
        else if (this.basemap === "bluemap") {
            this._map.removeLayer(this.mapgroup);
            this._map.removeLayer(this.satellitegroup);
            this._map.addLayer(this.bluemapgroup);
            this._map.fire('baselayerchange', this.mapgroup);
            this._map.fire('baselayerchange', this.satellitegroup);
        }
    }
});

L.control.basemaps = function (options) {
    return new L.Control.Basemaps(options);
};
